<template>
  <div class="wrapper">
    <!-- <Anchor :labelList="labels" activeId="row-01" labelWidth="100">
      <div class="line" id="row-01">
        <CasePlanInfoBase
          ref="CasePlanInfoBase"
          v-model="value"
          @formchange="CasePlanInfoBaseChange"
        />
      </div>
      <div class="line" id="row-02">
        <CaseExecuteInfoBase
          ref="CaseExecuteInfoBase"
          v-model="value"
          @formchange="CaseExecuteInfoBaseChange"
        />
      </div>
      <div class="line" id="row-03">
        <CaseSummaryInfoBase
          ref="CaseSummaryInfoBase"
          v-model="value"
          @formchange="CaseSummaryInfoBaseChange"
        />
      </div>
      <div class="line" id="row-04">
        <HistorySH ref="HistorySH" v-model="value" />
      </div>
    </Anchor>-->
    <BreakSpace :label="labels[0].title" :containerStyle="{ marginBottom: '10px' }" />
    <CaseBasicsInfoBase ref="CaseBasicsInfoBase" v-model="value"/>
    <BreakSpace :label="labels[1].title" :containerStyle="{ marginBottom: '10px' }" />
    <CasePlanInfoBase ref="CasePlanInfoBase" v-model="value" @formchange="CasePlanInfoBaseChange"/>
    <BreakSpace :label="labels[2].title" :containerStyle="{ marginBottom: '10px' }" />
    <CaseExecuteInfoBase
      ref="CaseExecuteInfoBase"
      v-model="value"
      @formchange="CaseExecuteInfoBaseChange"
    />
    <BreakSpace :label="labels[3].title" :containerStyle="{ marginBottom: '10px' }" />
    <CaseSummaryInfoBase
      ref="CaseSummaryInfoBase"
      v-model="value"
      @formchange="CaseSummaryInfoBaseChange"
    />
    <!-- <BreakSpace :label="labels[4].title" :containerStyle="{ marginBottom: '10px' }" /> -->
    <!-- <ReportInfoBase ref="ReportInfoBase" v-model="value" @formchange="ReportInfoBaseChange" /> -->
    <!-- <BreakSpace :label="labels[4].title" :containerStyle="{ marginBottom: '10px' }" />
    <BusinesOpinions ref="BusinesOpinions" v-model="value" @formchange="BusinesOpinionsChange" /> -->
    <BreakSpace :label="labels[4].title" :containerStyle="{ marginBottom: '10px' }" />
    <HistorySH v-model="value" />
    <div class="dialogBottmView">
      <el-button @click="closeDialog">关 闭</el-button>
      <el-button type="primary" :loading="saveLoading" @click="saveDialog">保 存</el-button>
      <el-button type="primary" :loading="determineLoading" @click="determineDialog">提 报</el-button>
      <el-button type="primary" :loading="finishLoading" @click="finishDialog">项目结束</el-button>
    </div>
  </div>
</template>

<script>
import { authority } from '@/utils/authMixin';
import { dictionary } from '@/utils/dictMixin';
import CaseBasicsInfoBase from './CaseBasicsInfoBase'; //项目基本信息
import CasePlanInfoBase from './CasePlanInfoBase'; //项目计划信息
import CaseExecuteInfoBase from './CaseExecuteInfoBase'; //项目执行信息
import CaseSummaryInfoBase from './CaseSummaryInfoBase'; //总结资料
// import ReportInfoBase from './ReportInfoBase'; //项目报备信息
import HistorySH from './HistorySH'; //历史审核记录
import { formatdateByDay } from '@/utils/index';
import { updateCaseSummary, finishCaseSummary, handCaseSummary, passCaseSummary } from '@/api/cms-api/projectOperating/plan/index';

export default {
  mixins: [authority, dictionary],
  components: {
    CaseBasicsInfoBase,
    CasePlanInfoBase,
    CaseExecuteInfoBase,
    CaseSummaryInfoBase,
    // BusinesOpinions,
    HistorySH
    // ReportInfoBase
  },
  props: {
    value: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      labels: [
        { title: '项目基本信息', id: 'row-01' },
        { title: '项目计划信息', id: 'row-02' },
        { title: '项目执行信息', id: 'row-03' },
        { title: '项目总结材料', id: 'row-04' },
        // { title: '项目报备信息', id: 'row-05' },
        // { title: '事业部意见', id: 'row-06' },
        { title: '历史审核记录', id: 'row-07' }
      ],
      value: {},
      formAllValue: {}, //两个表单内容
      saveLoading: false,
      determineLoading: false,
      finishLoading: false
    };
  },
  methods: {
    //项目结束
    async finishDialog() {
      this.$confirm('确定结束项目吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        this.finishLoading = true;
        let res = {};
        res = await finishCaseSummary(this.value.row.planId);
        if (res.resultCode === 200) {
          this.finishLoading = false;
          this.$notify.success({ title: '提示信息', message: '已结束!' });
          this.closeDialog('refresh');
        }
      });
      // let resLog = {};
      //日志添加
      // let data = {};
      // data = Object.assign(data, { planId: this.value.row.planId });
      //日志状态项目结束
      // data = Object.assign(data, { reviewStatus: 25311012 });
      // resLog = await passCaseSummary(data);
    },
    //提报（验证并提交）
    async determineDialog() {
      this.$refs.CaseExecuteInfoBase.saveDialog();
    },
    //保存（不验证）
    async saveDialog() {
      //取数据
      let allData = { ...this.value };

      //@1 项目计划信息
      this.$refs.CasePlanInfoBase.$refs.formPanel.excuteFormData(this.$refs.CasePlanInfoBase.$refs.formPanel.form);
      allData = Object.assign(allData, this.$refs.CasePlanInfoBase.$refs.formPanel.form);

      //@2 项目执行信息
      this.$refs.CaseExecuteInfoBase.$refs.formPanel.excuteFormData(this.$refs.CaseExecuteInfoBase.$refs.formPanel.form);
      allData = Object.assign(allData, this.$refs.CaseExecuteInfoBase.$refs.formPanel.form);
      
      //@3 项目总结材料
      this.$refs.CaseSummaryInfoBase.$refs.formPanel.excuteFormData(this.$refs.CaseSummaryInfoBase.$refs.formPanel.form);
      allData = Object.assign(allData, this.$refs.CaseSummaryInfoBase.$refs.formPanel.form);

      //@4 项目报备信息
      // this.$refs.ReportInfoBase.$refs.formPanel.excuteFormData(this.$refs.ReportInfoBase.$refs.formPanel.form);
      // allData = Object.assign(allData, this.$refs.ReportInfoBase.$refs.formPanel.form);

      allData = this.transformAllEnclosure(allData);
      let res = {};
      let resLog = {};
      allData = Object.assign(allData, { orderCompletionRate: allData.orderCompletionRate * 100 });
      allData = Object.assign(allData, { guestsCompletionRate: allData.guestsCompletionRate * 100 });
      allData = Object.assign(allData, { retentionCompletionRate: allData.retentionCompletionRate * 100 });
      //处理实际开展/投放时间
      if (allData.realLaunchReleaseTime != null && allData.realLaunchReleaseTime.length > 0) {
        let realLaunchReleaseTimeJson = '';
        for (let i in allData.realLaunchReleaseTime) {
          if (realLaunchReleaseTimeJson == '') {
            realLaunchReleaseTimeJson = formatdateByDay(allData.realLaunchReleaseTime[i]);
          } else {
            realLaunchReleaseTimeJson += ',' + formatdateByDay(allData.realLaunchReleaseTime[i]);
          }
        }
        allData.realLaunchReleaseTime = realLaunchReleaseTimeJson;
      }
      if (formatdateByDay(allData.realStartDate) > formatdateByDay(allData.realEndDate)) {
        this.$notify.warning({ title: '提示信息', message: '实际开始日期不能大于实际结束日期!' });
        return false;
      }
      this.saveLoading = true;
      //保存（类型存草稿功能）将项目总结状态变为总结未提交
      res = await updateCaseSummary(allData, this.value.row.planId);
      //保存操作日志
      //项目计划总结ID
      // let data = {};
      // data = Object.assign(data, { planId: this.value.row.planId });
      //日志状态总结未提交
      // data = Object.assign(data, { reviewStatus: 25311007 });
      // resLog = await passCaseSummary(data);
      if (res.resultCode === 200) {
        this.saveLoading = false;
        this.$notify.success({ title: '提示信息', message: '保存成功!' });
        this.closeDialog('refresh');
      }
    },

    //转化表单的附件 转换为list<Map<String,Object>>（提交前使用）
    transformAllEnclosure(data) {
      let rdata = Object.assign({}, data);
      rdata.photoUrl = this.transformEnclosureList(rdata.photoUrl); //现场高清照片
      rdata.cancellationUrl = this.transformEnclosureList(rdata.cancellationUrl); //项目核销材料
      rdata.feeDetailUrl = this.transformEnclosureList(rdata.feeDetailUrl); //实际使用费用明细
      rdata.contractOfferUrl = this.transformEnclosureList(rdata.contractOfferUrl); //合同报价单
      rdata.billUrl = this.transformEnclosureList(rdata.billUrl); //发票
      rdata.otherUrl = this.transformEnclosureList(rdata.otherUrl); //其它
      rdata.uploadFileList = this.transformEnclosureList(rdata.uploadFileList); //报备附件
      return rdata;
    },
    //转换成附件对象 转换为list<Map<String,Object>>
    transformEnclosureList(list) {
      let fileUrlList = [];
      for (let i = 0; i < list.length; i++) {
        fileUrlList.push(Object.assign({}, { name: list[i].name }, { url: list[i].url }));
      }
      return fileUrlList;
    },
    CaseExecuteInfoBaseChange(data) {
      this.formAllValue = Object.assign(this.formAllValue, data);
      this.$refs.CasePlanInfoBase.saveDialog();
    },
    CasePlanInfoBaseChange(data) {
      this.formAllValue = Object.assign(this.formAllValue, data);
      this.$refs.CaseSummaryInfoBase.saveDialog();
    },
    
    async CaseSummaryInfoBaseChange(data) {

      //处理实际开展/投放时间
      if (this.formAllValue.realLaunchReleaseTime != null && this.formAllValue.realLaunchReleaseTime.length > 0) {
        let realLaunchReleaseTimeJson = '';
        for (let i in this.formAllValue.realLaunchReleaseTime) {
          if (realLaunchReleaseTimeJson == '') {
            realLaunchReleaseTimeJson = formatdateByDay(this.formAllValue.realLaunchReleaseTime[i]);
          } else {
            realLaunchReleaseTimeJson += ',' + formatdateByDay(this.formAllValue.realLaunchReleaseTime[i]);
          }
        }
        this.formAllValue.realLaunchReleaseTime = realLaunchReleaseTimeJson;
      }
      this.formAllValue = Object.assign(this.formAllValue, data);
      this.formAllValue = this.transformAllEnclosure(this.formAllValue);
      this.formAllValue = Object.assign({ planStatus: 25311008 }, this.formAllValue);
      this.formAllValue = Object.assign(this.formAllValue, { orderCompletionRate: this.formAllValue.orderCompletionRate * 100 });
      this.formAllValue = Object.assign(this.formAllValue, { guestsCompletionRate: this.formAllValue.guestsCompletionRate * 100 });
      this.formAllValue = Object.assign(this.formAllValue, { retentionCompletionRate: this.formAllValue.retentionCompletionRate * 100 });
      this.determineLoading = true;
      let res = {};
      //提报项目总结，状态变更为总结已提交
      res = await handCaseSummary(this.formAllValue, this.value.row.planId);
      if (res.resultCode === 200) {
        this.determineLoading = false;
        this.$notify.success({ title: '提示信息', message: '提报成功!' });
        this.closeDialog('refresh');
      }
    },
    async ReportInfoBaseChange(data) {
      console.log(data,"=====1");
      this.formAllValue = Object.assign(this.formAllValue, data);
      this.formAllValue = this.transformAllEnclosure(this.formAllValue);
      this.formAllValue = Object.assign({ planStatus: 25311008 }, this.formAllValue);
      this.determineLoading = true;
      let res = {};
      // let resLog = {};
      //提报项目总结，状态变更为总结已提交
      res = await handCaseSummary(this.formAllValue, this.value.row.planId);
      //日志添加
      // let data1 = {};
      // data1 = Object.assign(data1, { planId: this.value.row.planId });
      //日志状态总结已提交
      // data1 = Object.assign(data1, { reviewStatus: 25311008 });
      // resLog = await passCaseSummary(data1);
      if (res.resultCode === 200) {
        this.determineLoading = false;
        this.$notify.success({ title: '提示信息', message: '提报成功!' });
        this.closeDialog('refresh');
      }
    },
    //取消操作
    // closeDialog() {
    //   this.$emit('close', { visible: false });
    // }
    closeDialog(param) {
      this.$emit('close', false, param);
    }
  }
};
</script>
<style scoped>
.wrapper {
  height: calc(100vh - 110px);
}
</style>
